{
  "name": "Retail Store Performance Variance Escalation Agent",
  "nodes": [
    {
      "parameters": { "updates": ["message"] },
      "id": "tg-trigger-c4",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "name": "user_message", "value": "={{ $json.message.text }}", "type": "string" },
            { "name": "user_name", "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}", "type": "string" },
            { "name": "chat_id", "value": "={{ $json.message.chat.id }}", "type": "string" }
          ]
        }
      },
      "id": "set-c4",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Retail Store Performance Variance Escalation Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new store performance variance or CHECK the status of an existing variance.\n\n2. For new variance registrations, collect:\n   - Store ID\n   - Performance Metric impacted (Sales / Footfall / Conversion / Shrinkage / Margin)\n   - Expected benchmark value\n   - Actual observed value\n   - Brief reason or observation\n\n3. Validate the store using the Store Master Tool and extract:\n   - Store_Name\n   - Region\n   - Store_Manager_Email (used for email communication)\n\n4. If the store is valid and active:\n   - Generate a unique variance reference ID in the format:\n     STR-YYYYMMDD-[last 4 digits of store]-HHmmss\n   - Register the variance using the Store Variance Register Tool\n   - Send an acknowledgement email using the Gmail Tool to Store_Manager_Email\n   - Clearly explain review workflow and expected timelines\n   - Respond using the Telegram Response Tool\n\n5. If the store is invalid or inactive:\n   - Politely explain the reason\n   - Do NOT register the variance\n   - Respond using the Telegram Response Tool\n\n6. For status checks:\n   - Ask for variance reference ID if missing\n   - Retrieve current status using the Store Variance Status Tool\n   - Explain the current review stage and next steps\n   - Respond using the Telegram Response Tool\n\nStrict rules:\n- Do NOT assign blame.\n- Do NOT promise corrective actions.\n- Store manager email must be retrieved from the master sheet only.\n- ALWAYS use the Telegram Response Tool."
        }
      },
      "id": "agent-c4",
      "name": "Store Performance Variance Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-c4",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-c4",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": { "descriptionType": "manual" },
      "id": "store-master-c4",
      "name": "Store Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": { "operation": "append", "descriptionType": "manual" },
      "id": "store-register-c4",
      "name": "Store Variance Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": { "descriptionType": "manual" },
      "id": "store-status-c4",
      "name": "Store Variance Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('store_manager_email') }}",
        "subject": "={{ $fromAI('email_subject') }}",
        "message": "={{ $fromAI('email_body') }}"
      },
      "id": "gmail-c4",
      "name": "Store Variance Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-c4",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [[{ "node": "Workflow Configuration", "type": "main" }]]
    },
    "Workflow Configuration": {
      "main": [[{ "node": "Store Performance Variance Agent", "type": "main" }]]
    },
    "Store Performance Variance Agent": {
      "main": [[{ "node": "Send Telegram Response", "type": "main" }]]
    }
  },
  "active": false
}
